'use strict';
app.factory('TokenService', function(HttpUtilService,$q,LocalStorageService,USERAUTHDATA,APPCONSTANT,$state) {
    /***
     * 获取用户token
     * @param loginType 认证类型,数字1表示使用用户名认证，数字2表示使用用户邮箱认证，数字3表示使用手机号认证
     * @param userId
     * @param password
     * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
     *{
          "code": "000000",
          "message": "验证通过",
          "data": {
            "content_length": 3,
            "access_token": "53d1eda456240b994c047ac9",
            "exprires_in": 733222
          },
          "status": true
        }
     */
    function getToken(loginType,userId,password){
      var deferred = $q.defer();
      HttpUtilService.httpPostNoToken("Token/Request",{SSO_type:loginType,customer_flag:userId,password:password})
     .then(function (response) {
       //{
       //   "code": "000000",
       //   "message": "验证通过",
       //   "data": {
       //     "content_length": 3,
       //     "access_token": "53d1eda456240b994c047ac9",
       //     "exprires_in": 733222
       //   },
       //   "status": true
       // }
       deferred.resolve(response.data)
      },function (error) {
        console.log("getToken error:"+ error)
      })
      return deferred.promise;
    }
     /***
     * 验证token有效性
     * @param token
     * @param userId
     * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
     */
    function checkToken (token,userId){
      var deferred = $q.defer();
      var url = APPCONSTANT.pathUrl + "Token/Request";
      HttpUtilService.httpPostHasToken("Token/Request",{access_token:token,user_id:userId},token)
     .then(function (response) {
        //{
        //   "code": "000000",
        //   "message": "验证通过",
        //   "data": {
        //     "content_length": 3,
        //     "access_token": "53d1eda456240b994c047ac9",
        //     "exprires_in": 733222
        //   },
        //   "status": true
        // }
          deferred.resolve(response.data)
      },function (error) {
        console.log("getToken error:"+ error)
      })
      return deferred.promise;
    }

    /**
     * 注销token用于loginout
     * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
     */
    function breakToken(token,userId){
      var deferred = $q.defer();
      HttpUtilService.httpPostHasToken("Token/Request",{access_token:token,user_id:userId},token)
      .then(function (response) {
        //{
        //response:1;是否成功（1：成功；0：未成功；-1：异常或不存在该请求）
        // }
        deferred.resolve(response.data)
      },function (error) {
        console.log("breakToken error:"+ error)
      })
      return deferred.promise;
    }
  function getDueToken (){
    var deferred = $q.defer();
    var authData = LocalStorageService.getObject(USERAUTHDATA);
    var nowDate = new Date().getTime();
    if(authData.dueTime && authData.dueTime > nowDate){
      deferred.resolve(authData)
    }else{
      deferred.reject(null);
    }
    return deferred.promise;
  }
  function getLocalToken(){
    var deferred = $q.defer();
    getDueToken().then(function (data) {
      deferred.resolve(data.token)
    }, function (err) {
      //deferred.reject(null)
      $state.go('login')
    })
    return deferred.promise;
  }
  function getLocalUserId(){
    var deferred = $q.defer();
    getDueToken().then(function (data) {
      deferred.resolve(data.user_id)
    }, function (err) {
      //deferred.reject(null)
      $state.go('login')
    })
    return deferred.promise;
  }
    return {
      getToken:getToken,
      checkToken:checkToken,
      breakToken:breakToken,
      getLocalToken:getLocalToken,
      getLocalUserId:getLocalUserId
    };
  })


